﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ABulletL : ABullet
{
    private Vector2 firePosition;
    private SpriteRenderer sr;
    private bool fired = false;
    public void SetFirePosition(Vector2 p)
    {
        this.firePosition = p;
    }
    private void Awake()
    {
        ABullet.AddBullet(BulletType.L, gameObject);
        rig = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
        Color color = sr.color;
        color.a = 0f;
        sr.color = color;
    }
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        CommonFly();
    }
    private void FixedUpdate()
    {
        Vector2 currentPosition = transform.position;
        // Ray2D ray = new(firePosition, this.direction);
        Vector2 v = (currentPosition - firePosition) * this.direction;
        Color color = sr.color;

        if (v.x >= 0 && v.y >= 0)
        {
            color.a = 255f;
            fired = true;
        }
        else
        {
            color.a = 0f;
        }
        sr.color = color;
    }

    protected override void OutOfLimitRange()
    {
        if (fired)
        {
            CreateBoom(transform.position);
            Disappear();
        }
    }

    private void OnDestroy()
    {
        ABullet.RemoveBullet(BulletType.L, gameObject);
        this.CommonOnDestroy();
    }

    protected override void Hit(GameObject target)
    {
        this.CommonHit(transform.position, target);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject target = collision.gameObject;
        Hit(target);
    }

    public static void ClearExistBullet()
    {
        ISet<GameObject> bulletSet = ABullet.GetBulletSet(ABullet.BulletType.L);
        foreach (GameObject bullet in bulletSet)
        {
            Destroy(bullet);
        }
        ABullet.ClearBulletSet(ABullet.BulletType.L);

    }
}
